<template>
    <div id="app">
       <router-view/>
    </div>
</template>

<script>

    import {request} from "@/netword/request";
    import New from '@/js/eventBus.js'
    export default {
        name: 'App',
        components: {

        },
        data(){
          return{
              showLoginButton:'',
          }
        },
        created() {
            New.$on("on-width", data => {
                this.isWidth=data
            });
        },
        methods:{
            beforeunloadHandler(){
                this._beforeUnload_time=new Date().getTime();
            },
            unloadHandler(e) {
                this._gap_time = new Date().getTime() - this._beforeUnload_time;
                //判断是窗口关闭还是刷新
                if (this._gap_time <= 5) {
                    //如果是登录状态，关闭窗口前，移除用户
                    if (!this.showLoginButton) {
                        this.$store.commit("setLogin",false)
                        this.$store.commit("setAccount",'')
                        localStorage.removeItem("login")
                        localStorage.removeItem("account")
                        //监控浏览器窗口关闭
                        request({
                            url: "/logout",
                            method: 'get',
                        }).then(res => {
                            this.$store.commit("setToken",'')
                            localStorage.removeItem("token")
                        }).catch(err=>{
                            console.log(err)
                        })

                    }
                }
            }
        },
        mounted() {
            window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
            window.addEventListener('unload', e => this.unloadHandler(e))
        },
        destroyed() {
            window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
            window.removeEventListener('unload', e => this.unloadHandler(e))
        },


    }
</script>

<style>
    @import "assets/css/index.css";

</style>
